<?php
namespace Model;
use DB;
use Pagination;

class Target extends \Model {
	public static function get_account($user_id, $per_page = 10, $offset = 0) {
		$query = DB::select()->from('targets');
		$query->where('user_id', '=', $user_id);      
		$query->order_by('id', 'desc');
		$query->limit($per_page);
		$query->offset($offset);
		$r = $query->execute();
		return $r->as_array();
	} 

	public static function get_cnt($user_id) {
		$query = DB::select()->from('targets');
		$query->where('user_id', '=', $user_id);      
		$r = $query->execute();
		return count($r->as_array());
	}

	public static function get_all_account() {
		$query = DB::select()->from('targets')->limit(10);
		$now = time();
		$query->where('touched_at', '<=', date('Y-m-d H:i:s', $now - 10 * 60));      
		$query->order_by('id', 'desc');
		$r = $query->execute();
		return $r->as_array();
	} 

	public static function add($user_message) {
		$data = $user_message;
		$query = DB::select()->from('targets');
		$query->where('idstr', '=', $data['idstr']);      
		$query->and_where('user_id', '=', $data['user_id']);      
		$r = $query->execute();
		if (count($r->as_array()) > 0) return true;

		DB::insert('targets')->set($data)->execute();
	}

	public static function update($user_message) {
		$data = $user_message;
		if (!$data) return;
		$query = DB::update('targets');
		$query->where('screen_name', '=', $data['screen_name']);      
		$query->set($data)->execute();
	}

}
